# AC_PREREQ(2.60)
m4_define([CPPUTILS_VERSION_STR],
          m4_bpatsubst(m4_esyscmd([cat ../../VERSION]),
                       [\([0-9.]*\)\(\w\|\W\)*],
                       [\1]))
AC_INIT([CppUtils],[CPPUTILS_VERSION_STR],[developers@latestbit.com])

AC_MSG_CHECKING([Checking version of library])
AC_MSG_RESULT(CPPUTILS_VERSION_STR)

CPPUTILS_PACKAGE=[${PACKAGE_NAME}]
AC_DEFINE_UNQUOTED(CPPUTILS_PACKAGE, "${CPPUTILS_PACKAGE}", [Name of package])
AC_SUBST(CPPUTILS_PACKAGE)

CPPUTILS_VERSION=[${PACKAGE_VERSION}]
AC_DEFINE_UNQUOTED(CPPUTILS_VERSION, "CPPUTILS_VERSION_STR", [Version number of package])
AC_SUBST(CPPUTILS_VERSION)

AM_INIT_AUTOMAKE([foreign])
AC_PROG_CXX
AC_PROG_LIBTOOL
AC_LANG_CPLUSPLUS
AC_REQUIRE_CPP

# Checking for OS
if [test "`uname | grep \"CYGWIN\"`" != ""]; then
	AC_DEFINE([HAVE_CYGWIN],[],[Cygwin environment detected])
fi

if [test "`uname | grep \"Darwin\"`" != ""]; then
        AC_DEFINE([HAVE_MACOSX],[],[MACOS is detected])
	OS_SPECIFIC_LDFLAGS=["-framework CoreServices"]
	AC_SUBST(OS_SPECIFIC_LDFLAGS)
fi
if [test "`uname | grep \"Linux\"`" != ""]; then
        AC_DEFINE([HAVE_LINUX],[],[Linux is detected])
	OS_SPECIFIC_LDFLAGS=["-ldl"]
	AC_SUBST(OS_SPECIFIC_LDFLAGS)
fi
if [test "`uname | grep \"Sun\"`" != ""]; then
        AC_DEFINE([HAVE_SUNOS],[],[SunOS is detected])
	OS_SPECIFIC_LDFLAGS=["-ldl -lrt"]
	AC_SUBST(OS_SPECIFIC_LDFLAGS)
fi

# User can configure paths to includes / libraries
AC_ARG_WITH([cppflags],[--with-cppflags=flags      User specific CPPFLAGS],[CPPFLAGS="$withval"])
AC_ARG_WITH([ldflags],[--with-ldflags=flags      User specific LDFLAGS],[LDFLAGS="$withval"])

# User can enable or disable some features
AC_ARG_ENABLE([debug],[--enable-debug=no      Build with debugging symbols (-g)],[CPPFLAGS="$CPPFLAGS -g"])

# Checking for Boost
AC_MSG_CHECKING([Boost C++ library postfix ])
BOOST_LIB_POSTFIX=[""]
AC_ARG_WITH([boostpostfix],[--with-boostpostfix=postfix      Boost library postfix],[BOOST_LIB_POSTFIX="$withval"])
AC_SUBST(BOOST_LIB_POSTFIX)
AC_MSG_RESULT([$BOOST_LIB_POSTFIX])

SAVE_LIBS=$LIBS
AC_MSG_CHECKING([for Boost C++ library (looking for library boost_thread$BOOST_LIB_POSTFIX)])

boost=no
LIBS=["$LIBS $OS_SPECIFIC_LDFLAGS -lboost_thread$BOOST_LIB_POSTFIX "]
AC_TRY_LINK([
	#include <boost/thread/thread.hpp>
	#include <boost/foreach.hpp>
], 
[
    boost::thread::thread *poThread;
],
boost=yes)
LIBS=$SAVE_LIBS

if test $boost = no; then
	AC_MSG_ERROR("Boost library not found in the include/library paths!")
else
	AC_MSG_RESULT(Ok)
fi

# Checking for std::wstring
AC_MSG_CHECKING([for std::wstring support in STL])
has_wstring=no
AC_TRY_COMPILE(
[
	#include <string>
],
[
	std::wstring sHello=L"Hello";
], has_wstring=yes)

if test $has_wstring = no; then
	AC_MSG_WARN("Your STL configuration doesn't support std::wstring")
else
	AC_DEFINE([HAVE_STL_WSTRING],[],[std::wstring support])
	AC_MSG_RESULT(Ok)
fi

# Checking for gethrtime
AC_MSG_CHECKING([for gethrtime support])
has_thrtime=no
AC_TRY_COMPILE(
[	
	#include <time.h>
	#include <sys/time.h>
],
[
	gethrtime();
], has_thrtime=yes)

if test $has_thrtime = no; then
	AC_MSG_RESULT(No)
else
	AC_DEFINE([HAVE_THRTIME],[],[gethrtime support])
	AC_MSG_RESULT(Ok)
fi

# Checking for clock_gettime
AC_MSG_CHECKING([for clock_gettime support])
has_clock_gettime=no
AC_TRY_COMPILE(
[	
	#include <time.h>
	#include <sys/time.h>
],
[
	struct timespec ts;

	clock_gettime(CLOCK_REALTIME,&ts);
], has_clock_gettime=yes)

if test $has_clock_gettime = no; then
	AC_MSG_RESULT(No)
else
	AC_DEFINE([HAVE_CLOCK_GETTIME],[],[clock_gettime support])
	AC_MSG_RESULT(Ok)
fi

# Checking for backtrace_symbols
AC_MSG_CHECKING([for backtrace_symbols support])
has_backtrace_symbols=no
AC_TRY_COMPILE(
[	
  #include <execinfo.h>
  #include <stdio.h>
],
[
	void* callstack[128];
    int i, frames = backtrace(callstack, 128);
    char** strs = backtrace_symbols(callstack, frames);
], has_backtrace_symbols=yes)

if test $has_backtrace_symbols = no; then
	AC_MSG_RESULT(No)
else
	AC_DEFINE([HAVE_BACKTRACE_SYMBOLS],[],[backtrace_symbols support])
	AC_MSG_RESULT(Ok)
fi

AC_CHECK_LIB(gmp, __gmpz_init, , [AC_MSG_ERROR(
[GNU MP Library not found])])


CPPFLAGS=["$CPPFLAGS -Wall"]

AM_CONFIG_HEADER([../../cpputils/include/cpputils/config/cpputils-config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
